package com.bqtt.tumor.demo.presentation.controller;

import javax.validation.Valid;

import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.bqtt.tumor.demo.business.CityService;
import com.bqtt.tumor.demo.business.HealthUserService;
import com.bqtt.tumor.demo.business.OccupationService;
import com.bqtt.tumor.demo.entity.HealthUser;
import com.bqtt.tumor.demo.entity.SystemUser;

public class health_user {

	
	public static String queryHealthUser( SystemUser user,  ModelMap model, @ModelAttribute("condition") HealthUser condition)	
	{
		if(user.getSessionState() ==0)
		{// 用户未登入
			if(!model.containsAttribute("user"))
				model.addAttribute("user", new SystemUser());
			return "login";
		}
		
		if(!model.containsAttribute("new_health_user"))
		     model.addAttribute("new_health_user", new HealthUser());		
		if(!model.containsAttribute("edit_health_user"))
		     model.addAttribute("edit_health_user", new HealthUser());	

		model.addAttribute("historySearchCondition", condition);
		
		model.addAttribute("condition", condition); //保留上次查找的字段
		
	    model.addAttribute("health_user_list", HealthUserService.searchByUserCondition(condition, 20, 1));
		
	    //城市列表
	    model.addAttribute("city_list",CityService.getList());
	    
	    //职业列表
	    model.addAttribute("occupation_list", OccupationService.getList());
	    
	    if(!model.containsAttribute("isAddNewShow"))
	    model.addAttribute("isAddNewShow","false");
		
	  
		return "admin/health_user";
	}
	
	
	public static String addHealthUser( SystemUser user, HealthUser new_health_user,  RedirectAttributes passModel,BindingResult bindingResult){	

		//session 变量 ——不用自动添加到Model
		//condition currentUser
		
		if(bindingResult!=null && bindingResult.hasErrors())
		{
			//出错的话，就保持弹出窗口，显示错误
			passModel.addFlashAttribute("isAddNewShow","true");
			// 其他变量需要手动添加到Model
			passModel.addFlashAttribute("new_health_user", new_health_user);
			
			return "redirect:searchHealthUser";
		}
		
		if(user.getSessionState() ==0)
		{// 用户未登入
			 return  "redirect:/";
		}
		

		boolean result = HealthUserService.addUser(new_health_user);
		
		if(result)
		{		
			passModel.addFlashAttribute("information", "<font style='color:#5599FF'>操作成功！</font>");		
		}
		else
		{
			passModel.addFlashAttribute("information", "<font style='color:#FF3333'>操作失败！</font>");
		}
		
		passModel.addFlashAttribute("isAddNewShow","false");

		return "redirect:searchHealthUser";
		
	}
	
	
	
	public static String editHealthUser(SystemUser user, RedirectAttributes model, HealthUser edit_health_user, BindingResult bindingResult){	

		//session 变量 ——不用自动添加到Model
		//condition currentUser/
		

			if(bindingResult!=null && bindingResult.hasErrors())
			{
				//出错的话，就保持弹出窗口，显示错误
				return "redirect:searchHealthUser";
			}
			
			if(user.getSessionState() ==0)
			{// 用户未登入
				return "redirect:/";
			}
			

			boolean result = HealthUserService.updateUser(edit_health_user);
			
			if(result)
			{	
				model.addFlashAttribute("information", "<font style='color:#5599FF'>操作成功！</font>");
			}
			else
			{
				model.addFlashAttribute("information", "<font style='color:#FF3333'>操作失败！</font>");
			}
			
			return "redirect:searchHealthUser";
		}

}
